package wfc.dao.daoImpl;


import wfc.dao.UserDao;
import wfc.entity.User;
import wfc.util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl  implements UserDao {
    @Override
    public boolean add(User user) {
        boolean flag=false;
        try {
            String sql="insert into user(username,password) values(?,?)";
            Object[] params={user};
            int i= BaseDao.userInsert(sql, params);
            //（4）处理执行结果
            if(i>0){
                System.out.println("注册成功");
            }
            flag=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

//    @Override
//    public List<User> getNewsList() {
//        List<User> newList=new ArrayList<User>();
//        try {
//            //（3）获得Statement对象，执行SQL语句
//            String sql="select id,username,password,phone,avatar from user ";
//            Object[] params={};
//            ResultSet rs=this.executeSQL(sql, params);
//            //（4）处理执行结果(ResultSet)，
//            while(rs.next()){
//
//
//
//
//                String username=rs.getString("username");
//                String password=rs.getString("password");
//                String phone=rs.getString("phone");
//                String avatar=rs.getString("avatar");
//
//
//                //封装成新闻信息对象
//                User user=new User();
//                user.setUsername(username);
//                user.setPassword(password);
//                user.setPhone(phone);
//                user.setAvatar(avatar);
//
//
//                //将新闻对象放进集合中
//                newList.add(user);
//            }
//        }catch (SQLException e) {
//
//            e.printStackTrace();
//        }finally{
//            //释放资源
//            this.closeResource();
//        }
//        return newList;
//    }

    @Override
    public User ServiceByUsername(String username) {
        ResultSet resultSet = null;
        User news_user = new User();
        try {
            String  sql = "SELECT * FROM `user` WHERE username =?";
            Object[] obj={username};

            resultSet = BaseDao.userSelectByName(sql, obj);
            while(resultSet.next()) {
                int id1 = resultSet.getInt("id");
                String userName = resultSet.getString(2);
                String password = resultSet.getString(3);
                String phone = resultSet.getString(4);
                String avatar = resultSet.getString(5);
                news_user.setId(id1);
                news_user.setUsername(userName);
                news_user.setPassword(password);
                news_user.setPhone(phone);
                news_user.setAvatar(avatar);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            BaseDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return news_user;
    }
}
